package com.example.stars_project_android.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.stars_project_android.R;
import com.example.stars_project_android.entity.MessageUser;

import java.util.List;

/**
 * 私信列表的适配器
 */
public class MessageListViewAdapter extends BaseAdapter {

    private Context mContext;
    private int layoutResId;
    private List<MessageUser> messageUserList;

    public MessageListViewAdapter(Context mContext, int layoutResId, List<MessageUser> messageUserList) {
        this.mContext = mContext;
        this.layoutResId = layoutResId;
        this.messageUserList = messageUserList;
    }

    @Override
    public int getCount() {
        if (messageUserList != null){
            return messageUserList.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (messageUserList != null){
            return messageUserList.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null){
            //加载item的布局文件，生成一个View对象，再给converView赋值
            convertView = LayoutInflater.from(mContext).inflate(layoutResId,null);
            //获取每一个item中的控件对象
            viewHolder = new ViewHolder();
            viewHolder.img = convertView.findViewById(R.id.iv_users);
            viewHolder.userId = convertView.findViewById(R.id.userId);
            viewHolder.date = convertView.findViewById(R.id.post_time);
            viewHolder.msgData = convertView.findViewById(R.id.information);
            //将ViewHolder对象同converView关联起来
            convertView.setTag(viewHolder);
        }else {//重复利用convertView对象
            //获取到跟当前的convertView绑定在一起的ViewHolder对象
            viewHolder = (ViewHolder) convertView.getTag();
        }
        //给控件对象绑定要显示的数据
        viewHolder.img.setImageResource(messageUserList.get(position).getImg());
        viewHolder.userId.setText(messageUserList.get(position).getUserId());
        viewHolder.date.setText(messageUserList.get(position).getDate());
        viewHolder.msgData.setText(messageUserList.get(position).getMsgData());
        return convertView;
    }
    private static class ViewHolder{
        //每一个item中的控件
        ImageView img;
        TextView userId, date, msgData;
    }
}
